<?php

namespace App\Http\Controllers\Home;

use App\Messages;
use function Couchbase\defaultDecoder;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\News;

use Illuminate\Http\Response;
use App\Carousel;

use MercurySeries\Flashy\Flashy;

class IndexController extends Controller
{
    //

    public function __construct(){
        $this->carousels = Carousel::whereIn('sort',[1,2,3])->orderBy('sort','asc')->get();
    }
    public function index()
    {
        $works = News::where('is_publish',1)->where('channel_id',500)->orderBy('created_at','desc')->limit(5)->get();
        $societys = $this->get_news(301,6);
        $notices = $this->get_news(102,6);
        $policys = $this->get_news(103,6);
        $informations = $this->get_news(104,6);
        $activitys = $this->get_news(105,6);
        $item1 = $this->get_news(901,3);
        $item2 = $this->get_news(902,3);
        $item3 = $this->get_news(903,3);
        $item4 = $this->get_news(904,3);
        $item5 = $this->get_news(905,6);
        $item6 = $this->get_news(602,3);
        $item7 = $this->get_news(604,3);
        $carousels = $this->carousels;
        $member = $this->get_one_new(701);

        // dd($item5);exit();
    	return view('index.index',compact('works','societys','notices','policys','informations','activitys','item1','item2','item3','item4','item5','item6','item7','member','carousels'));
    }

    public function overview()
    {

        $item1 = $this->get_one_new(201);
        $item2 = $this->get_one_new(202);
        $item3 = $this->get_one_new(203);
        $item4 = $this->get_one_new(204);
        $item5 = $this->get_one_new(205);
        $carousels = $this->carousels;

    	return view('index.overview',compact('item1','item2','item3','item4','item5','carousels'));
    }

    public function information()
    {
        $item1 = $this->get_news(301);
        $item2 = $this->get_news(102);
        $item3 = $this->get_news(103);
        $item4 = $this->get_news(104);
        $item5 = $this->get_news(305);
        $carousels = $this->carousels;

    	return view('index.information',compact('item1','item2','item3','item4','item5','carousels'));
    }

    public function apply()
    {
        $item1 = $this->get_one_new(401);
        $item2 = $this->get_one_new(402);
        $item3 = $this->get_one_new(403);
        $item4 = $this->get_one_new(404);
        $item5 = $this->get_one_new(405);
        $item6 = $this->get_one_new(406);
        $carousels = $this->carousels;

    	return view('index.apply',compact('item1','item2','item3','item4','item5','item6','carousels'));
    }

    public function work()
    {
        $item1 = $this->get_news(501);
        $item2 = $this->get_news(502);
        $item3 = $this->get_news(503);
        $item4 = $this->get_news(500);
        $carousels = $this->carousels;
    
    	return view('index.work',compact('item1','item2','item3','item4','carousels'));
    }

    public function member_service()
    {
        $item1 = $this->get_news(901);
        $item2 = $this->get_news(602);
        $item3 = $this->get_news(902);
        $item4 = $this->get_news(604);
        $item5 = $this->get_news(903);
        $item6 = $this->get_news(904);
        $carousels = $this->carousels;

    	return view('index.member_service',compact('item1','item2','item3','item4','item5','item6','carousels'));
    }

    public function member_exchange()
    {
        $item1 = $this->get_news(701);
        $item2 = $this->get_news(905);
        $item3 = $this->get_news(703);
        $item4 = $this->get_news(704);
        $carousels = $this->carousels;
        
    	return view('index.member_exchange',compact('item1','item2','item3','item4','carousels'));
    }

    public function message()
    {
        $messages = News::where('is_publish',1)->where('channel_id',801)->orderBy('created_at','desc')->limit(4)->get();
        $carousels = $this->carousels;
    	return view('index.message' ,compact('messages','carousels'));
    }

    public function message_store(Request $request)
    {
        Messages::create($request->all());
//        echo "<script>alert('留言成功')</script>";
        Flashy::success('留言成功');
        return redirect()->back();
    }

    public function article_detail(News $article)
    {

        $article->channel;
        $carousels = $this->carousels;
//        dd($carousels);
    	return view('index.article_detail',compact('article','carousels'));
    }

    public function get_news($type,$limit=''){
        if(empty($limit)){
            $works = News::where('is_publish',1)->where('channel_id',$type)->orderBy('created_at','desc')->get();
        }else{
            $works = News::where('is_publish',1)->where('channel_id',$type)->limit($limit)->orderBy('created_at','desc')->get();
        }
        return $works;
    }

    public function get_one_new($type){
        $works = News::where('channel_id',$type)->orderBy('created_at','desc')->first();
        return $works;
    }

    public function download_file(){

        return response()->download(public_path('download/广东省人力资源管理协会企业入会申请表.pdf'));
    }
}
